package com.jwthhealth_pub.old.physicalfitness.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.physicalfitness.view.IndividualHeadPortraitActivity;

/**
 * Created by Administrator on 2017/1/14.
 */
public class PortraitAdapter extends RecyclerView.Adapter {

    private IndividualHeadPortraitActivity activity;
    private int[] imgs = new int[]{R.mipmap.portrait_1, R.mipmap.portrait_2, R.mipmap.portrait_3,
            R.mipmap.portrait_4, R.mipmap.portrait_5, R.mipmap.portrait_6,
            R.mipmap.portrait_7, R.mipmap.portrait_8, R.mipmap.portrait_9,
            R.mipmap.portrait_10, R.mipmap.portrait_11, R.mipmap.portrait_12};

    public int getSelectPos() {
        return selectPos;
    }

    public int[] getImgs() {
        return imgs;
    }

    private int selectPos = -1;
    
    public PortraitAdapter(IndividualHeadPortraitActivity individualHeadPortrait) {
        this.activity = individualHeadPortrait;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(activity, R.layout.item_portrait, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.imageView.setBackgroundResource(imgs[position]);

        if (position == selectPos) {
            viewHolder.selected.setVisibility(View.VISIBLE);
        } else {
            viewHolder.selected.setVisibility(View.GONE);
        }

        viewHolder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectPos = position;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return imgs.length;
    }

    private class ViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView, selected;
        
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.img);
            selected = (ImageView) itemView.findViewById(R.id.iv_seclected);
        }
    }
}
